#include "VxIrrLib.h"
///BspToBulletConverter  extends the BspConverter to convert to Bullet datastructures. In this case, Irrlicht swaps Z and Y, so we do the same.

//============================================================================
//=== constructor ===//
IrrPhysBspBulletConverter::IrrPhysBspBulletConverter( IrrPhysBase * poPhysBase )
	: m_poPhysBase(poPhysBase)
{
}

//============================================================================
//! this function overrides the BspConverter::addConvexVerticesCollider 
void IrrPhysBspBulletConverter::addConvexVerticesCollider(btAlignedObjectArray<btVector3>& vertices, bool isEntity, const btVector3& entityTargetLocation)
{
	///perhaps we can do something special with entities (isEntity)
	///like adding a collision Triggering (as example)
	
	if (vertices.size() > 0)
	{
		///irrlicht swaps Z and Y in a BSP file?
		btAlignedObjectArray<btVector3> rotatedVerts;
		for (int i=0;i<vertices.size();i++)
		{
			rotatedVerts.push_back(btVector3(vertices[i].getX(),vertices[i].getZ(),vertices[i].getY())* m_poPhysBase->m_f32PhysWorldScaling );
		}
		float mass = 0.f;
		btTransform startTransform;
		//can use a shift
		startTransform.setIdentity();
		startTransform.setOrigin(btVector3(0,0,-10.f));
		//this create an internal copy of the vertices
		
		btCollisionShape* shape = new btConvexHullShape(&(rotatedVerts[0].getX()),rotatedVerts.size());
		m_poPhysBase->m_aoPhysCollisionShapes.push_back(shape);

		btRigidBody* body = new btRigidBody(0.f,0,shape);
		//body->setCenterOfMassTransform(btTransform(btQuaternion::getIdentity(),btVector3(-1300,-144,-1249)));
		m_poPhysBase->m_poPhysDynamicsWorld->addRigidBody( body );
	}
}
